Refactor Internal Time Retrieval Handling for Improved Consistency and Resolution #19202
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This pull request refactors how PHP internally handles current time retrieval and time measurement, introducing a more consistent and portable approach. The primary goals are to encapsulate platform-specific differences, improve time resolution, and prepare for long-term compatibility (e.g., Y2038 on WIN64).
Key Changes
Introduced
zend_time.h
A new internal header that abstracts system time functions, replacing direct usage of
<time.h>
and related platform-specific APIs.Added
zend_realtime_spec
A unified wrapper around
clock_gettime()
,timespec_get()
,gettimeofday()
, andtime()
using the real/wall clock.Returns a
timespec
structure with nanosecond precision (when available).Defined
zend_realtime_get
macroA lightweight macro wrapping
time(NULL)
for simple, low-resolution time retrieval.Introduced
zend_monotime_fallback
A macro that maps to
zend_hrtime
or falls back tozend_realtime_spec
.Useful for time measurements (e.g. timeout handling) where monotonic time is preferred, but wall time is an acceptable fallback.
Provided helper macros
Added utilities to simplify usage of
timeval
andtimespec
structures.Replaced direct system time API usage
Internal calls to system time functions now use
zend_realtime_*
orzend_monotime_fallback
.Standardized time representation
Transitioned to
timespec
for current time where appropriate, while retainingtimeval
for files and stream-related functionality.Benefits
Improved Portability and Readability
Platform-specific time logic is encapsulated, reducing conditionals and improving clarity.
Guaranteed Time API Availability
The new abstraction ensures time can always be retrieved via a fallback chain:
clock_gettime()
→timespec_get()
→gettimeofday()
→time()
Y2038 Compatibility on WIN64
Addresses issues caused by
long
intimeval.tv_sec
on 64-bit Windows systems.Improved Resolution for Time Functions
microtime()
andgettimeofday(true)
now offer better time precision without breaking backward compatibility.Removed Redundant Availability Checks
Previously unverified use of
gettimeofday()
has been replaced by a robust fallback mechanism.Conditional checks for
microtime()
,gettimeofday()
, anduniqid()
have been removed.